package j2se.socket;

import java.io.*;
import java.net.Socket;

/**
 * Created by jingqing.zhou on 2015/6/17.
 */
public class client {
    public static void main(String[] args) {
        try {
            //创建客户端socket，指定服务器地址和端口
            Socket socket = new Socket("localhost",8999);
            //获取输出流，用来向服务器端发送信息
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os);//将输出流包装为打印流
            pw.write("用户名：admin1111；密码：123");
            pw.flush();
            socket.shutdownOutput();//关闭输出流
            //获取输入流，并读取服务器端的响应信息
            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String info = null ;
            //循环读取服务器端信息
            while ((info = br.readLine())!=null){
                System.out.printf("我是客户端，服务器端说："+ info);
            }
            socket.shutdownInput();//关闭输入流
            br.close();
            is.close();
            pw.close();
            os.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
